// @flow
// deviceFile condition.js 页面 chart state
import moment from 'moment'

export const CHANGE_HISTORY_CHART_COND = 'CHANGE_HISTORY_CHART_COND'  // 改变渲染历史图的条件
export const CHANGE_DYNAMIC_CHART_COND = 'CHANGE_DYNAMIC_CHART_COND'

export const CHANGE_DYNAMIC_DATE = 'CHANGE_DYNAMIC_DATE'
export const OPEN_WEBSOCKET = 'OPEN_WEBSOCKET'
export const CLOSE_WEBSOCKET = 'CLOSE_WEBSOCKET'
export const SEND_WEBSOCKET_MSG = 'SEND_WEBSOCKET_MSG'

type HistoryCond = {
  monitors: Array<string>,
  startTime: moment,
  endTime: moment
}

type Action = {
  type: string,
  historyCond: HistoryCond,
  dynamicCond: Array<string>,
  dynamicData: Array<any>
}

export const changeHistoryChartCond = (historyCond: Object) => {
  return {
    type: CHANGE_HISTORY_CHART_COND,
    historyCond
  }
}

export const changeDynamicChartCond = (dynamicCond: Array<string>) => {
  return {
    type: CHANGE_DYNAMIC_CHART_COND,
    dynamicCond
  }
}

export const changeDynamicDateCond = (dates: Object) => {
  return {
    type: CHANGE_DYNAMIC_DATE,
    dates
  }
}

export const openWebsocket = () => {
  return {
    type: OPEN_WEBSOCKET
  }
}

export const closeWebsocket = () => {
  return {
    type: CLOSE_WEBSOCKET
  }
}

export const sendWebsocketMsg = () => {
  return {
    type: SEND_WEBSOCKET_MSG
  }
}

// function websocketInit (onmessage) {
//   websocket = new WebSocket(`${__WEBSOCKET_API__}real-time`)
//   websocket.onopen = function (evt) {
//     websocket.send(JSON.stringify({
//       fieldGroupId: 'sinter_draft_fan', // typeEn
//       fields: ['field1', 'name2'] // kmxVariableNameEn
//     }))
//     console.log('Websocket open.')
//   }
//   websocket.onclose = function (evt) {
//     console.log('Websocket close.')
//   }
//
//   websocket.onmessage = (evt) => {
//     onmessage(evt)
//   }
//   websocket.onerror = function (evt) {
//     console.error('Websockt error!')
//   }
// }

const ACTION_HANDLERS = {
  [CHANGE_HISTORY_CHART_COND]: (state, { historyCond }: Action) => Object.assign({}, state, {
    historyCond: historyCond
  }),
  [CHANGE_DYNAMIC_CHART_COND]: (state, { dynamicCond }: Action) => Object.assign({}, state, {
    dynamicCond: dynamicCond
  }),
  [CHANGE_DYNAMIC_DATE]: (state, action) => Object.assign({}, state, {
    dynamicDate: action.dates
  }),
  [OPEN_WEBSOCKET]: (state) => {
    // websocketInit((evt: Object) => {
    //   return Object.assign({}, state, {
    //     dynamicData: JSON.parse(evt.data)
    //   })
    // })
    return state
  },
  [CLOSE_WEBSOCKET]: (state) => {
    // websocket && websocket.close()
    return state
  },
  [SEND_WEBSOCKET_MSG]: (state) => {
    // websocket && websocket.send(JSON.stringify({
    //   fieldGroupId: 'sinter_draft_fan', // typeEn
    //   fields: ['field1', 'name2'] // kmxVariableNameEn
    // }))
    return state
  }
}

type State = Object

const initialState = {
  kmxMonitors: [],
  historyCond: {
    monitors: [],
    startTime: moment().subtract(20, 'minute'),
    endTime: moment()
  },
  dynamicDate: {
    startTime: moment().subtract(20, 'minute'),
    endTime: moment()
  },
  dynamicCond: [],
  dynamicData: []
}

export default function reducer (state: State = initialState, action: Action) {
  const handler = ACTION_HANDLERS[action.type]

  return handler ? handler(state, action) : state
}
